package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.DepartmentApplyAddDTO;
import com.oa.admin.dto.DepartmentApplyListDTO;
import com.oa.admin.entity.DepartmentApply;
import com.oa.admin.service.IDepartmentApplyService;
import com.oa.admin.vo.DepartmentApplyByDepartmentIdVO;
import com.oa.admin.vo.DepartmentApplyDetailsVO;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/departmentApply")
@Api(description = "部门活动申请表模块")
public class DepartmentApplyController extends BaseController {

    private final IDepartmentApplyService departmentApplyService;

    @Autowired
    public DepartmentApplyController(IDepartmentApplyService departmentApplyService) {
        this.departmentApplyService = departmentApplyService;
    }


    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp list(DepartmentApplyListDTO departmentApplyListDTO) {
        departmentApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(departmentApplyService.findDepartmentApplyList(departmentApplyListDTO));
    }


    /**
     * 新增
     *
     * @param departmentApplyAddDTO DepartmentApplyAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "部门活动申请对象") @RequestBody(required = false) DepartmentApplyAddDTO departmentApplyAddDTO) throws BusinessException, DataIsEmptyException {
        departmentApplyAddDTO.setProposer(getLoginUserId());
        return departmentApplyService.add(departmentApplyAddDTO);
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("根据ID查询")
    @ApiOperation(value = "根据ID查询")
    public Resp<DepartmentApplyDetailsVO> findDepartmentApplyDetails(@PathVariable String id){
        return new Resp<DepartmentApplyDetailsVO>().success(departmentApplyService.findDepartmentApplyDetails(id));
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审核")
    @ApiOperation(value = "审核")
    public Resp audit(
            @ApiParam("申请ID") @RequestParam String id,
            @ApiParam("状态(1通过 0不通过)") @RequestParam Integer status) throws DataIsEmptyException {
        return departmentApplyService.audit(id, status);
    }

    @GetMapping("/department/{id}")
    @CommonController
    @Description("根据部门ID查询")
    @ApiOperation(value = "根据部门ID查询")
    public Resp<List<DepartmentApplyByDepartmentIdVO>> findByDepartmentId(@PathVariable String id){
        return new Resp<List<DepartmentApplyByDepartmentIdVO>>().success(departmentApplyService.findByDepartmentId(id));
    }

    /**
     * 根据Id修改
     *
     * @param departmentApply DepartmentApply
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "部门活动申请表对象") @RequestBody(required = false) DepartmentApply departmentApply) {
        if (departmentApplyService.updateById(departmentApply)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        DepartmentApply departmentApply = departmentApplyService.selectById(id);
        if (departmentApply != null && departmentApply.getIsDeleted() == 2) {
            departmentApply.setIsDeleted(1);
            if (departmentApplyService.updateById(departmentApply)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
